package it.softphone.commons.service.security.shiro;


import it.softphone.commons.service.security.SecurityAccountService;
import it.softphone.commons.service.security.SecurityAccountServiceFactory;

import java.security.GeneralSecurityException;

import org.apache.commons.configuration.Configuration;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.util.Factory;

/**
 * 
 * @author softphone
 *
 */
//@org.kohsuke.MetaInfServices
public class ShiroSecurityAccountServiceFactory extends SecurityAccountServiceFactory {

	private static final String SHIRO_DEFAULT_CONFIG_FILE = "classpath:shiro-default.ini";

	public ShiroSecurityAccountServiceFactory() {
	}

	@Override
	public SecurityAccountService newSecurityAccountService(Configuration cfg) 	throws GeneralSecurityException {
		final  String iniResourcePath = ( cfg==null ) ? 
				SHIRO_DEFAULT_CONFIG_FILE :
				cfg.getString("iniResourcePath", SHIRO_DEFAULT_CONFIG_FILE);
		
		Factory<SecurityManager> factory = new IniSecurityManagerFactory(iniResourcePath);

        SecurityManager securityManager = factory.getInstance();

        SecurityUtils.setSecurityManager(securityManager);
        
        //DefaultSecurityManager sm = (DefaultSecurityManager) securityManager;

        //sm.setSubjectFactory( new MySubjectFactory() );

        return new ShiroSecurityAccountService( null );
	}

}
